package com.sdp.lease.db.l_location_info.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.lease.db.l_location_info.entity.LLocationInfo;
import com.sdp.lease.db.l_location_info.mapper.LLocationInfoMapper;
import com.sdp.lease.db.l_location_info.service.ILLocationInfoService;
import com.sdp.lease.utils.LeaseConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-24
 */
@Service
public class LLocationInfoServiceImpl extends ServiceImpl<LLocationInfoMapper, LLocationInfo> implements ILLocationInfoService {

	@Override
	public List<LLocationInfo> queryLocationByLevel(String locationLevel) {
		LambdaQueryWrapper<LLocationInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LLocationInfo::getLocationLevel, locationLevel);
		queryWrapper.orderByAsc(LLocationInfo::getLocationOrder);
		return list(queryWrapper);
	}

	@Override
	public List<JSONObject> createLocationTreeSelect() {
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		LambdaQueryWrapper<LLocationInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByAsc(LLocationInfo::getLocationNo);
		List<LLocationInfo> locationList = list(queryWrapper);
		List<LLocationInfo> rootList = new ArrayList<LLocationInfo>();
		List<String> rootKeys = new ArrayList<String>();
		if(locationList!=null && locationList.size()>0) {
			for(LLocationInfo l : locationList) {
				if(StringUtils.isEmpty(l.getLocationParentNo()) || LeaseConstants.否.getCode().equals(l.getLocationParentNo())) {
					rootList.add(l);
					rootKeys.add(l.getKeyId());
				}
			}
		}
		
		if(rootList!=null && rootList.size()>0) {
			
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = new JSONObject(true);
				parentObj.put("label", rootList.get(i).getLocationName());
				parentObj.put("value", rootList.get(i).getLocationNo());
				treeList.add(parentObj);
				getChildLocation(parentObj, locationList);
			}
		}
		
		return treeList;
	}
	
	
	@SuppressWarnings({ "unchecked" })
	private void getChildLocation(JSONObject parentObj,List<LLocationInfo> locationList) {
		List<Object> childList = new ArrayList<Object>();
	
		for(int j=0;j<locationList.size();j++) {
			if(!StringUtils.isEmpty(locationList.get(j).getLocationParentNo()) && parentObj.getString("value").equals(locationList.get(j).getLocationParentNo())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				
				JSONObject childObj = new JSONObject(true);
				childObj.put("label", locationList.get(j).getLocationName());
				childObj.put("value", locationList.get(j).getLocationNo());
				childList.add(childObj);
				getChildLocation(childObj, locationList);
			}
			
		}
	}

	
}
